package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.SocietyDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Society;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 *
 */
public class SocietyListFragment extends BaseListFragment<SocietyListFragment.SocietyItemViewHolder, Society> {
    //搜索类型
    public int type;
    //只检索我参与的社团
    public static final int TYPE_MY_SOCIETY = 0;
    //检索推荐给我的社团
    public static final int TYPE_RECOMMEND_SOCIETY = 1;
    //根据关键字搜索社团
    public static final int TYPE_SEARCH_SOCIETY = 2;
    //某个标签下面的社团
    public static final int TYPE_TAG_SOCIETY = 3;

    private String keyword = "";

    private Long tagid = null;


    public interface ClickSocietyListener {

        public void societyItemClick(Long sid);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt("type");
        tagid = getArguments().getLong("tagid");
    }

    /**
     * 创建
     *
     * @return
     */
    public static SocietyListFragment create(int type) {
        SocietyListFragment fragment = new SocietyListFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 创建我参加的社团列表
     *
     * @return
     */
    public static SocietyListFragment createMySocietyListFragment(Long uid) {
        if (uid == null || uid == 0)
            uid = UserPreference.getUser().uid;
        SocietyListFragment fragment = new SocietyListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", TYPE_MY_SOCIETY);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static SocietyListFragment createTagSocietyListFragment(Long tagid) {
        SocietyListFragment fragment = new SocietyListFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("tagid", TYPE_TAG_SOCIETY);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 刷新
     *
     * @param keyword
     */
    public void research(String keyword) {
        this.keyword = keyword;
        refresh();
    }

    /**
     * 创建推荐给我的社团列表
     */
    public static SocietyListFragment createRecommendListFragment() {
        SocietyListFragment fragment = new SocietyListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", TYPE_RECOMMEND_SOCIETY);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 根据关键字搜索的社团列表
     */
    public static SocietyListFragment createSearchListFragment() {
        SocietyListFragment fragment = new SocietyListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", TYPE_SEARCH_SOCIETY);
        fragment.setArguments(bundle);
        return fragment;
    }


    public SocietyListFragment() {
    }


    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认
        return null;
    }

    public void forwardDetail(Long sid) {
        Intent intent = new Intent(getActivity(), SocietyDetailActivity.class);
        intent.putExtra("sid", sid);
        startActivity(intent);
    }

    @Override
    protected SocietyItemViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_society_item, parent, false);
        SocietyItemViewHolder viewHolder = new SocietyItemViewHolder(view, new ClickSocietyListener() {
            @Override
            public void societyItemClick(Long sid) {
                forwardDetail(sid);
            }
        });
        return viewHolder;
    }

    @Override
    protected SocietyItemViewHolder createfootViewHolder(View view) {
        return new SocietyItemViewHolder(view);
    }


    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<Society>>(new TypeReference<Result<List<Society>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
                addResult(null);

            }

            @Override
            public void responseInfo(Result<List<Society>> data) {
                addResult(data);
            }
        });
    }


    @Override
    protected String setUrlBeforeRequest(List<Society> datas, Pagination pagination) {
        String url = "";
        String temp = "";
        if (type == TYPE_MY_SOCIETY)
            url = ApiUtil.getApi(context, R.array.api_my_joined_society, pagination.getPageNo(), pagination.getPageSize());
        else if (type == TYPE_RECOMMEND_SOCIETY)
            url = ApiUtil.getApi(context, R.array.api_recommend_society, pagination.getPageNo(), pagination.getPageSize());
        else if (type == TYPE_TAG_SOCIETY)
            url = ApiUtil.getApi(context, R.array.api_recommend_society, tagid, pagination.getPageNo(), pagination.getPageSize());
        else if (type == TYPE_SEARCH_SOCIETY) {
            try {
                temp = java.net.URLEncoder.encode(keyword, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            url = ApiUtil.getApi(context, R.array.api_search_society, pagination.getPageNo(), pagination.getPageSize());
            url = url + "&keyword=" + temp;
        } else if (type == TYPE_TAG_SOCIETY) {
            url = ApiUtil.getApi(context, R.array.api_search_society_tag, tagid, pagination.getPageNo(), pagination.getPageSize());
        }
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }


    public static class SocietyItemViewHolder extends BaseRecycleViewHolder<Society> {
        public NetworkImageView mThum;
        public TextView mName;
        public TextView mDesc;
        public TextView joinNum;
        private View view;
        private ClickSocietyListener listener;

        public SocietyItemViewHolder(View itemView, ClickSocietyListener listener) {
            this(itemView);
            this.listener = listener;
        }


        public SocietyItemViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
            this.view = itemView;
        }

        public void initWidget(View view) {
            mThum = (NetworkImageView) view.findViewById(R.id.niv_thum);
            mName = (TextView) view.findViewById(R.id.tv_name);
            mDesc = (TextView) view.findViewById(R.id.tv_desc);
            joinNum = (TextView) view.findViewById(R.id.tv_joinnum);
        }


        @Override
        public void renderView(final Society data, int position) {
            mThum.setImageUrl(data.thumurl, HttpUtils.getImageLoader());
            mName.setText(data.sname);
            mDesc.setText(data.description);
            joinNum.setText("成员:  " + data.attach.joinnum + "");
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.societyItemClick(data.sid);
                }
            });

        }
    }


}
